home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / QuickDraw GX / SetDefaultDTP / FSSetPrinter.c next >
Encoding:
C/C++ Source or Header  |  1995-12-13  |  4.0 KB  |  142 lines  |  [TEXT/CWIE]

  1. /********************************************************************************
  2.     FSSetPrinter.c
  3.     
  4.     This file does the deed of constructing the AppleEvent that tells the 
  5.     finder which desktop printer to select as the new default printer.
  6.     
  7.     ©1995  Apple Computer, Inc.
  8.     All rights reserved.
  9.  
  10. ********************************************************************************/
  11.  
  12.  
  13. #include <AppleEvents.h>
  14. #include <PrintingMessages.h>
  15. #include <GXExceptions.h>
  16.  
  17. #include "FSSetPrinter.h"
  18.  
  19.  
  20. // ----------------------------------------------------------------------
  21. /*
  22.     Create the Finder address we wish to send to - note that we mean here "Finder" in the
  23.     most generic sense.  In reality, this can be the Finder or AtEase.
  24. */
  25.  
  26. static OSErr GetFinderAddr(AEAddressDesc *theDesc)
  27. {
  28.     OSErr                result = noErr;
  29.     ProcessInfoRec        processInfo;
  30.     ProcessSerialNumber    serialNumber;
  31.     
  32.  
  33.     serialNumber.highLongOfPSN = 0;
  34.     serialNumber.lowLongOfPSN = kNoProcess;
  35.     
  36.     while (result = (GetNextProcess(&serialNumber) == noErr))
  37.         {
  38.         processInfo.processInfoLength = sizeof(ProcessInfoRec);
  39.         processInfo.processName     = nil;
  40.         processInfo.processAppSpec     = nil;
  41.         
  42.         result = GetProcessInformation(&serialNumber, &processInfo);
  43.         if (result == noErr)
  44.             {
  45.             if ( processInfo.processType == 'FNDR' )
  46.                 {
  47.                 result = AECreateDesc(typeProcessSerialNumber, (Ptr)&serialNumber, sizeof(ProcessSerialNumber), theDesc);
  48.                 return(result);
  49.                 }
  50.             }
  51.         }
  52.  
  53.     return(result);
  54.     
  55. } // GetFinderAddr
  56.  
  57.  
  58. // ----------------------------------------------------------------------
  59. /*
  60.     Routine to send an AppleEvent to the Finder.
  61.     Note that the data is sub-typed so the Finder can decipher the contents.
  62. */
  63.  
  64. OSErr    SASendAEToFinder(
  65.     Ptr                    dataPtr,         // -> AppleEvent data
  66.     Size                dataSize)        // size of AppleEvent data
  67. {
  68.     OSErr            result;
  69.     AEAddressDesc    finderAddr;        // the address of the Finder as an AppleEvent Descriptor
  70.     AppleEvent        theEvent;
  71.     AppleEvent        replyEvent;
  72.     
  73.     
  74.     result = GetFinderAddr(&finderAddr);
  75.     nrequire(result, Fail_GetAddr);
  76.     
  77.     result = AECreateAppleEvent(
  78.         kCoreEventClass,            // this is a core event
  79.         kFinderExtension,            // for a Finder extension
  80.         &finderAddr,                // and we send it to the Finder
  81.         kAutoGenerateReturnID,        // we aren't getting a return
  82.         kAnyTransactionID,            // and we don't care about the transaction #
  83.         &theEvent);                    // and we create it right here
  84.     nrequire(result, Fail_MakeEvent);
  85.             
  86.     result = AEPutParamPtr(
  87.         &theEvent,                    // the event to shove into
  88.         keyDirectObject,            // direct object keyword
  89.         kFinderExtension,            // for the Finder extension
  90.         dataPtr,                    // here's the data!
  91.         dataSize);                    // here's how long it is!
  92.     nrequire(result, Fail_StuffParm);
  93.     
  94.     result = AESend(
  95.         &theEvent,                    // send the status event
  96.         &replyEvent,                // no reply event because -
  97.         kAENoReply +                 // we don't want a reply
  98.          kAECanInteract +             // the receiver can interact with user
  99.          kAEDontReconnect,            // and don't bother to reconnect on error
  100.         kAENormalPriority,            // just a normal event
  101.         kAEDefaultTimeout,            // we'll wait some reasonable amount of time
  102.         nil, nil);                    // and don't care what happens during that.
  103.  
  104.  
  105. //    Exception Handling and drop through cleanup
  106.  
  107. Fail_StuffParm:
  108.     // now get rid of the AppleEvent
  109.     (void) AEDisposeDesc(&theEvent);
  110.  
  111. Fail_MakeEvent:
  112.     // and get rid of the Finder address
  113.     (void) AEDisposeDesc(&finderAddr);
  114.  
  115. Fail_GetAddr:
  116.  
  117.     // Fall through and return errror, if any
  118.     return(result);
  119.     
  120. } // SASendAEToFinder
  121.  
  122.  
  123. // ----------------------------------------------------------------------
  124. /*
  125.     This function constructs the data block that gets sent to the finder
  126.     via SASendAEToFinder containing the destination of the AppleEvent, which
  127.     event it is (kSetDefaultPrinterType) and the name of the desktop printer.
  128. */
  129.  
  130. OSErr SendTestAE(StringPtr dtpName)
  131. {
  132.     OSErr            err=noErr;
  133.     SetDTPEvent        myEvent;
  134.     
  135.     myEvent.pfeCreator = kPrintingExtension;            // to finder
  136.     myEvent.extensionType = kSetDefaultPrinterType;        // change default printer
  137.     BlockMove(dtpName,&myEvent.dtpName,dtpName[0]+1);    // copy name of new default printer
  138.         
  139.     err = SASendAEToFinder((Ptr) &myEvent,sizeof(SetDTPEvent));
  140.             
  141.     return (err);
  142. }